package com.hzwq.demo2.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzwq.demo2.role.service.IRoleService;
import com.hzwq.demo2.user.entity.User;
import com.hzwq.demo2.user.service.IUserService;
import com.hzwq.demo2.util.PageDataResult;
import com.hzwq.demo2.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zhaochunlin
 * @since 2020-08-13
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;
    @Autowired
    IRoleService roleService;



    @RequestMapping("/logout")
    public ModelAndView logout(HttpSession session, HttpServletRequest request, HttpServletResponse response) throws IOException {
        ModelAndView mav = new ModelAndView("/index/login");
        session.removeAttribute("user");
        return mav;
    }

    @RequestMapping("/loginplus")
    public ModelAndView  toIndex(HttpSession session,String username,String password) {
        ModelAndView mav =null;
        System.out.println(username);
        System.out.println(password);
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        if (StringUtils.isNotBlank(username)) {
           wrapper.eq("user_name",username);
            if (StringUtils.isNotBlank(password)) {
                wrapper.eq("pass_word", password);
                User one = userService.getOne(wrapper);

                if(one != null){
                    session.setAttribute("user",one);
                    mav=new ModelAndView("index/index");
                } else {
                    mav=new ModelAndView("index/login");
                }
            }
        }
        return mav;
    }

//    @RequestMapping("/login")
//    public ModelAndView toLogin() {
//        ModelAndView mav = new ModelAndView("index/login");
//        return mav;
//    }

    @RequestMapping("/index")
    public ModelAndView toIndex() {
        ModelAndView mav = new ModelAndView("index/index");
        return mav;
    }



    @RequestMapping("/list")
    public ModelAndView toList() {
        ModelAndView mav = new ModelAndView("user/list");
        return mav;
    }

    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ResponseBody
    public PageDataResult query(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit, String userName,
                                String passWord) {
        PageDataResult pdr = new PageDataResult();
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        if (StringUtils.isNotBlank(userName)) {
            wrapper.like("user_name", userName);
        }
        if (StringUtils.isNotBlank(passWord)) {
            wrapper.like("pass_word", passWord);
        }
        PageHelper.startPage(page, limit);
        List<User> list = userService.list(wrapper);
        PageInfo<User> pageInfo = new PageInfo<User>(list);
        pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
        pdr.setList(list);
        return pdr;
    }

    @RequestMapping("/edit")
    @ResponseBody
    public ModelAndView toEdit(Integer id) {
        ModelAndView mav = new ModelAndView("user/edit");
        User user = new User();
        if (id != null) {
            user = userService.getById(id);
        }
        mav.addObject("user", user);
        mav.addObject("roleList", roleService.list());
        return mav;
    }

    @RequestMapping("/updateRole")
    @ResponseBody
    public ModelAndView updateRole(Integer id) {
        ModelAndView mav = new ModelAndView("user/role-assignment");
        User user = new User();
        if (id != null) {
            user = userService.getById(id);
        }
        mav.addObject("user", user);
        mav.addObject("roleList", roleService.list());
        return mav;
    }




    @RequestMapping("/save")
    @ResponseBody
    public Result<User> save(User user) {
        userService.saveUserAndRole(user);
        return new Result<User>("0", "保存成功");
    }



    @RequestMapping("/add")
    @ResponseBody
    public Result<User> addUser(@RequestBody User user) {
        System.out.println(user);
        userService.save(user);
        return new Result<User>("200", "保存成功");
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ResponseBody
    public Result<User> updateUser(@RequestBody User user) {
        System.out.println(user);
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("id",user.getId());
        userService.update(user,wrapper);
        return new Result<User>("200", "更新成功");
    }

    @RequestMapping(value = "/delById/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Result<User> deleteUser(@PathVariable("id") Integer id) {
        System.out.println(id);
        userService.delById(id);
        return new Result<User>("200", "删除成功");
    }

    @RequestMapping("/del")
    @ResponseBody
    public Result<User> del(Integer id) {
        if (id != null) {
            userService.removeById(id);
        }
        return new Result<User>("0", "删除成功");
    }

    @RequestMapping("/detail")
    @ResponseBody
    public ModelAndView toDetail(Integer id) {
        ModelAndView mav = new ModelAndView("user/detail");
        User user = new User();
        if (id != null) {
            user = userService.getById(id);
        }
        mav.addObject("user", user);
        mav.addObject("roleList", roleService.list());
        return mav;
    }
}
